<template>
	<view class="login-container">
		
		
		<view class="login-logo-container" @click="chooseAvatar">
			<image class="login-logo" :src="avatarUrl" mode="scaleToFill"></image>
			
			<text class="avatar-tips">点击更换头像</text>
		</view>

		<view class="login-form-container">
			<form class="login-form" @submit="login">
				<view class="form-item">
					<text class="form-label">用户：</text>
					<input class="login-input" type="text" placeholder="请输入账号" 
						v-model="account" placeholder-style="color:#cecece;font-weight: bold;" />
				</view>
				
				<view class="form-item">
					<text class="form-label">密码：</text>
					<input class="login-input" type="text" placeholder="请输入密码" 
						v-model="password" placeholder-style="color:#cecece;font-weight: bold;" password="true" />
				</view>

				<view class="btn-group">
					<button class="reset-btn" @click="resetForm">重置</button>
					<button class="login-btn" form-type="submit">登录</button>
				</view>

				<view class="register-btn-container">
					<navigator url="/pages/register/register" open-type="navigate">
						<button class="register-btn">注册</button>
					</navigator>
				</view>
			</form>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				account: '',
				password: '',
				
				avatarUrl: '/static/logo.png'
			}
		},
		methods: {
			
			chooseAvatar() {
				uni.chooseImage({
					count: 1, 
					sizeType: ['original', 'compressed'], 
					sourceType: ['album', 'camera'],
					success: (res) => {
						
						this.avatarUrl = res.tempFilePaths[0];
						
					},
					fail: (err) => {
						uni.showToast({
							icon: 'error',
							title: '图片选择失败'
						});
						console.log('选择图片失败：', err);
					}
				});
			},

			resetForm() {
				this.account = '';
				this.password = '';
			},

			login() {
				const account = this.account;
				const password = this.password;
				
				if (account === "") {
					uni.showToast({
						icon: "error",
						title: '账号不能为空',
						duration: 2000,
						mask: true
					});
					return;
				}
				if (password === "") {
					uni.showToast({
						icon: "error",
						title: '密码不能为空',
						duration: 2000,
						mask: true
					});
					return;
				}
				uni.showLoading({
					title: '登录中',
					mask: true
				});

				uni.setStorage({
					data: account,
					key: "account",
				})
				setTimeout(function() {
					uni.hideLoading();
					uni.navigateBack();
				}, 2000);
			}
		}
	}
</script>

<style>
	.login-container {
		height: 100vh;
		display: flex;
		flex-direction: column;
		justify-content: center;
		padding: 0 40rpx;
	}

	.login-logo-container {
		display: flex;
		flex-direction: column; 
		justify-content: center;
		align-items: center;
		margin-bottom: 50rpx;
		
		cursor: pointer;
	}

	.login-logo {
		border-radius: 50%;
		width: 200rpx;
		height: 200rpx;
		
		border: 2rpx solid #eee;
	}

	
	.avatar-tips {
		font-size: 24rpx;
		color: #999;
		margin-top: 10rpx;
	}

	.login-form-container {
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		width: 100%;
	}

	.login-form {
		width: 100%;
	}

	.form-item {
		display: flex;
		align-items: center;
		margin-top: 30rpx;
	}
	.form-label {
		font-size: 28rpx;
		width: 120rpx;
	}

	.login-input {
		flex: 1; 
		height: 80rpx;
		border-radius: 20rpx;
		background-color: white;
		text-align: center;
		font-size: 28rpx;
	}

	.btn-group {
		display: flex;
		justify-content: space-between;
		margin-top: 50rpx;
	}
	.reset-btn {
		width: 48%;
		background-color: #fff;
		border: 1px solid #cecece;
		border-radius: 20rpx;
		font-size: 30rpx;
	}
	.login-btn {
		width: 48%;
		background-color: #0099ff;
		color: white;
		border-radius: 20rpx;
		font-size: 30rpx;
	}

	.register-btn-container {
		margin-top: 30rpx;
		width: 100%;
	}
	.register-btn {
		width: 100%;
		background-color: transparent;
		color: #ff0000;
		border: 1px solid #ff0000;
		border-radius: 20rpx;
		font-size: 30rpx;
	}
</style>